﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;

namespace Foursquare
{
    public partial class MainPage : PhoneApplicationPage
    {
        GeoCoordinateWatcher watcher;
        FoursquareClient fsq;
        bool hasData = false;

        public MainPage()
        {
            InitializeComponent();

            // Initialize Location Service
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
            switch (watcher.Status)
            {
                case GeoPositionStatus.Disabled:
                    {
                        MessageBox.Show("Location Service is disabled");
                        break;
                    }
            }
            watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            watcher.Start();

            // Initialize Foursquare Client
            fsq = new FoursquareClient();
            fsq.GetVenuesCompleted +=
                new FoursquareClient.GetVenuesCompletedEventHandler(fsq_GetVenuesCompleted);
        }

        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Ready : watcher.Start();break;
            }
        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (!hasData)
            {
                fsq.GetVenuesNearbyAsync(watcher.Position.Location.Latitude, watcher.Position.Location.Longitude);
                progress.Visibility = Visibility.Visible;
            }
        }

        void fsq_GetVenuesCompleted(object sender, List<Venue> venues)
        {
            nearbyList.ItemsSource = venues;
            hasData = true;
            progress.Visibility = Visibility.Collapsed;
            bingMap.Children.Clear();
            foreach (Venue venue in venues)
            {
                bingMap.Children.Add(new Pushpin()
                {
                    Content = venue.name,
                    Location = new GeoCoordinate(venue.location.lat, venue.location.lng)
                });
            }
        }

        private void refresh_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            progress.Visibility = Visibility.Visible;
            fsq.GetVenuesNearbyAsync(watcher.Position.Location.Latitude, watcher.Position.Location.Longitude);
        }

        private void nearbyList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if ((sender as ListBox).SelectedIndex == -1) return;
            Venue venue = (sender as ListBox).SelectedItem as Venue;
            //GeoCoordinate venueCoord = new GeoCoordinate(venue.location.lat, venue.location.lng);
            //bingMap.SetView(venueCoord, 15);
            //mainpano.DefaultItem = mainpano.Items[1];
            (App.Current as App).venueItem = venue;
            NavigationService.Navigate(new Uri("/ItemDetailPage.xaml", UriKind.RelativeOrAbsolute));
            (sender as ListBox).SelectedIndex = -1;
        }
    }
}